package cn.blnp.net.flowable.boot.customer.flowable.event.listener;

import cn.blnp.net.flowable.boot.customer.flowable.event.CustomFlowableEventType;
import cn.blnp.net.flowable.boot.customer.flowable.event.service.FlowableTaskUrgingEvent;
import lombok.extern.slf4j.Slf4j;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
import org.flowable.common.engine.api.delegate.event.FlowableEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEventListener;
import org.flowable.common.engine.api.delegate.event.FlowableEventType;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.flowable.task.service.impl.persistence.entity.TaskEntity;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

/**
 * <h3>自定义催办事件监听器</h3>
 *
 * @author <a href="mailto:blnp.yibin@qq.com">lyb</a>
 * @version 1.0
 * @since 2025/6/6 16:32
 */
@Slf4j
@Component
public class TaskUrgingEventListener implements FlowableEventListener {

    @Override
    public void onEvent(FlowableEvent event) {
        FlowableEventType eventType = event.getType();
        if (eventType.equals(FlowableEngineEventType.CUSTOM)) {
            if (event instanceof FlowableTaskUrgingEvent) {
                FlowableTaskUrgingEvent flowableTaskUrgingEvent = (FlowableTaskUrgingEvent) event;
                Object entityObject = flowableTaskUrgingEvent.getEntity();
                if (flowableTaskUrgingEvent.getCustomFlowableEventType().equals(CustomFlowableEventType.TASK_URGING)) {
                    TaskEntity task = (TaskEntity)entityObject;
                    log.info("{}被催办了！", task.getName());
                }
            }
        }
    }

    @Override
    public boolean isFailOnException() {
        return false;
    }

    @Override
    public boolean isFireOnTransactionLifecycleEvent() {
        return false;
    }

    @Override
    public String getOnTransaction() {
        return null;
    }
}
